//app.js
var Bmob = require('dist/Bmob-1.6.0.min.js')
const {
  $Message
} = require('dist/base/index');
App({
  onLaunch: function() {
    let that = this

    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    Bmob.initialize("b065d51925248d15f228d93358df3c99", "f277cda96ac3047703a42bf6864a60ed");



    // 登录
    wx.login({
      success: res => {
        // console.log('发送 res.code 到后台换取 openId, sessionKey, unionId',res)
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
      }
    })
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              that.globalData.userInfo = res.userInfo;
              Bmob.User.upInfo(that.globalData.userInfo);
              that.globalData.isLogin = true


              Bmob.User.auth().then(res1 => {
                //在此保存一下用户的性别 因为Bmob.User.upInfo没有自动上传
                console.log(res1)
                this.globalData.authData = res1;
                console.log('一键登陆成功')
                const query = Bmob.Query("UserData");
                query.statTo("where", '{"user":{"$inQuery":{"where":{"objectId":"' + that.globalData.authData.objectId + '"},"className":"_User"}}}')
                const pointer_user = Bmob.Pointer('_User');
                const poiID_user = pointer_user.set(that.globalData.authData.objectId)
                query.find().then(res2 => {

                  let gender = true;
                  if (res.userInfo.gender == 2)
                  { gender = false }
                  if (res2.length) {
                    console.log('查询成功，存在数据，准备更新性别：', res2[0].objectId)
                    res2.set('user', poiID_user);
                    res2.set('gender', gender)

                    res2.saveAll().then(res2 => {
                      console.log("刷新性别信息成功", res2)
                    })

                  } else {
                    console.log('查询成功，不存在数据，准备新增数据表存储性别')
                    const query1 = Bmob.Query("UserData")
                    query1.get(that.globalData.authData.objectId).then(res => {
                      query1.set('user', poiID_user);
                      query1.set('gender', gender)

                      query1.save().then(res2 => {
                        console.log("新建性别信息成功", res2)
                      })
                    })

                  }
                })

              }).catch(err => {
                console.log(err)
              });

              if (getCurrentPages().length) {
                $Message({
                  content: '登陆成功',
                  type: 'success'
                });
                that.globalData.isLoginShowed = true
              }
              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })
    console.log('app.js:onLaunch', this.globalData.userInfo)

  },
  globalData: {
    userInfo: null,
    authData: null,
    isLogin: false,
    isLoginShowed: false
  }
})